﻿
$$.Page = new function () {
	var me = this;
	var emptyfunc = function () { };
	// var pagesNode = $("#Pages");
	var Doc = document.body || document.documentElement;

	me.GetQueryParams = function (hash) {
		var q = hash || location.hash;
		if (!q) return null;
		if (q[0] === "#") q = q.substr(1);
		if (q[0] === "~") return null;
		if (!q) return null;
		var arr = q.split("&");
		if (!arr || arr.length < 1) return null;
		var data = {};
		var sidx = 0;
		var p = arr[0];
		if (p && p.length > 0 && p.indexOf("=") < 0) {
			var arr1 = p.split(".");
			if (arr1) {
				if (arr1[0]) {
					data.Page = arr1[0].trim().toLowerCase();
					// if (data.Page.charAt(0) === "^") {
					// 	data.Dialog = true;
					// 	data.Page = data.Page.substr(1);
					// }
				}
				if (arr1[1]) data.Option = $$.ParseInt(arr1[1]);
			}
			sidx = 1;
		}
		data.Params = {};
		for (var i = sidx; i < arr.length; i++) {
			var s = arr[i];
			if (!s) continue;
			var idx = s.indexOf("=");
			if (idx < 1 || idx >= s.length - 1) continue;
			var n = s.substr(0, idx).trim();
			if (!n) continue;
			var s = s.substr(idx + 1).trim();
			if (!s) continue;
			data.Params[n.toLowerCase()] = window.decodeURI(s);
		}
		return data;
	};


	var pageCur = null;
	var pages = [];
	var pageZIndex = 100;
	var pageZIndexF = 100100;

	function Closes(page){
		if(page && page !== pageCur && page.Closed !== true) page.Close();
		if(pages.length < 1) return;
		var pgs = pages;
		pages = [];
		var len = pgs.length;
		for(var i=0; i<len; i++){
			page = pgs[i];
			if(!page || page === pageCur || page.Closed === true) continue;
			page.Close();
		}
	}

	//name：页面名称（必须）
	//params：传递的参数，object类型
	//callback：回调函数，新开页面与发起页面通讯的方法
	//effect：过渡效果，int类型。100:对话框
	me.Open = function (name, params, callback, effect) {
		if (!name) return;
		var last = pageCur;
		if(last) {
			if(last.Handler && last.Handler.OnClose() === false) return;
			pages.push(last);
		}

		name = name.toLowerCase();

		var page = document.createElement("div");
		page.classList.add("Page");
		page.Handler = null;
		page.Effect = effect;
		page.Close = function () {
			$(page).remove();
			page.Closed = true;
		};
		if(effect>=100 || name === "login") {
			page.classList.add("full");
			page.style.zIndex = ++pageZIndexF;
		}
		else page.style.zIndex = ++pageZIndex;
		
		pageCur = page;
		pages.push(last);

		var frm = document.createElement("iframe");
		frm.Params = params;
		frm.Callback = callback;
		frm.Ready = function (handler) {
			if (!handler) { page.Close(); return; }
			page.Handler = handler;
			if(effect>=100 || name === "login" || handler.IsDialog) {
				if(!page.classList.contains("full")) 
				{
					page.classList.add("full");
					page.style.zIndex = ++pageZIndexF;
				}
				return;
			}
			Closes(page);
		};
		frm.Close = page.Close;
		frm.src = name + ".html?" + $$.AppVer;

		page.appendChild(frm);
		Doc.appendChild(page);
	}

	me.OpenHref = function (hash, callback) {
		var q = me.GetQueryParams(hash);
		if (q) me.Open(q.Page, q.Params, callback, q.Option);
	};

	var hash = location.hash;
	$(window).on("hashchange", function (evt) {
		if (!location.hash || location.hash === hash) return;
		if (location.hash[0] === "~" || location.hash[1] === "~") return;
		hash = location.hash;
		location.hash = "";
		me.OpenHref(hash);
	});

	//关闭当前打开的所有页面
	me.CloseAll = function () {
		$("iframe").remove();
		ageZIndex = 1001;
		pageCur = null;
	};
};

